<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import type { Product } from '@/types/product'
import { productService } from '@/services/apiService'
import { ElInput, ElButton, ElCard, ElMessage } from 'element-plus'
import LoadingSpinner from '@/components/LoadingSpinner.vue'

// 页面数据
const router = useRouter()
const products = ref<Product[]>([])
const searchKeyword = ref('')
const currentPage = ref(1)
const totalPages = ref(1)
const loading = ref(false)

// 获取商品列表
const fetchProducts = async () => {
  loading.value = true
  try {
    const response = await productService.getProducts(currentPage.value, searchKeyword.value)
    products.value = response.results
    totalPages.value = response.total_pages // 每页50个商品
  } catch (error) {
    console.error('获取商品列表失败:', error)
    ElMessage.error('获取商品列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索商品
const handleSearch = () => {
  currentPage.value = 1
  fetchProducts()
}

// 跳转到商品详情
const goToDetail = (id: number) => {
  console.log('Navigating to product detail with id:', id)
  router.push(`/product/${id}/`)
}

// 页面初始化
onMounted(() => {
  fetchProducts()
})
</script>

<template>
  <div class="product-list-container">
    <h1>校园二手市场</h1>
    <div class="search-bar">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索商品..."
        @keyup.enter="handleSearch"
        style="width: 300px; margin-right: 10px;"
      />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>
    <div class="product-grid">
      <el-card
        v-for="product in products"
        :key="product.gid"
        class="product-card"
        @click="goToDetail(product.gid)"
      >
        <template #header>
          <div class="product-header">
            <h3>{{ product.title }}</h3>
          </div>
        </template>
        <div class="product-content">
          <img :src="'http://localhost:8000/media/' + product.images[0]" alt="商品图片" />
          <p class="price">¥{{ product.price }}</p>
          <p class="view-count">浏览量: {{ product.view_count }}</p>
          <p class="seller">{{ product.seller_username }}</p>
          <el-button type="success" size="small">聊一聊</el-button>
        </div>
      </el-card>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <LoadingSpinner message="加载中..." />
      </div>

      <!-- 无数据状态 -->
      <div v-if="!loading && products.length === 0" class="no-data">
        <p>暂无商品信息</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.product-list-container {
  max-width: 100%;
  margin: 0;
  padding: 20px;
  width: 100%;
}

.search-bar {
  display: flex;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 10px;
}

.search-bar input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
  min-width: 200px;
}

.search-bar button {
  padding: 10px 20px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  width: 100%;
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  transition: box-shadow 0.3s ease;
  display: flex;
  flex-direction: column;
  width: 100%;
}

.product-card:hover {
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.product-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 4px;
  margin-bottom: 10px;
  flex: 1;
}

.price {
  font-size: 1.2em;
  color: #d9534f;
  font-weight: bold;
  margin: 5px 0;
}

.view-count {
  font-size: 0.85em;
  color: #6c757d;
  margin: 3px 0;
}

.seller {
  color: #6c757d;
  font-size: 0.9em;
  margin: 5px 0;
}

.contact-btn {
  width: 100%;
  padding: 8px;
  background: #28a745;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

.contact-btn:hover {
  background: #218838;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .product-list-container {
    padding: 15px;
  }

  .search-bar {
    flex-direction: column;
  }

  .search-bar input {
    border-radius: 4px;
    min-width: auto;
  }

  .search-bar button {
    border-radius: 4px;
    width: 100%;
  }

  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 15px;
  }

  .product-card img {
    height: 150px;
  }

  .price {
    font-size: 1.1em;
  }
}

@media (max-width: 480px) {
  .product-list-container {
    padding: 10px;
  }

  .product-grid {
    grid-template-columns: 1fr;
    gap: 15px;
  }

  .product-card img {
    height: 180px;
  }

  .product-card {
    padding: 12px;
  }
}

@media (min-width: 1200px) {
  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}
</style>
